﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Playlist_file_loader.Utilities
{
    class FileSystemUtilities
    {
        //-------------------------------------------------------------------//
        //-------------------------member variables--------------------------//
        //-------------------------------------------------------------------//

        private Core.ObjectContainer m_fileList;
        private String m_strTargetDir;
        private MainForm mainForm;

        //-------------------------------------------------------------------//
        //-------------------------function definitions----------------------//
        //-------------------------------------------------------------------//

        public FileSystemUtilities(MainForm mainForm )
        {
            this.mainForm = mainForm;
        }


        //-------------------------------------------------------------------//
        // Description: ...
        public void CopyFileListToDirectory(Core.ObjectContainer filelist, String strTargetDir)
        {
            this.m_fileList = filelist;
            this.m_strTargetDir = strTargetDir;

            Thread copyFileListToDirectoryThread = new Thread( RunCopyFileListToDirectory );
            copyFileListToDirectoryThread.IsBackground = true;
            copyFileListToDirectoryThread.Start();

        }

        //-------------------------------------------------------------------//
        // Description: ...
        private void RunCopyFileListToDirectory()
        {
            File currentFile;
            double progressBarStepSize = 100.0 / m_fileList.GetCount();
            double progressbarStepSizeSum = 0.0;
            for (int index = 0; index < m_fileList.GetCount(); index++)
            {
                try
                {
                    currentFile = (File)m_fileList.Get(index);
                    System.IO.Directory.CreateDirectory(m_strTargetDir);
                    String path = m_strTargetDir + currentFile.m_strFileName;
                    System.IO.File.Copy(currentFile.m_SrcPath, m_strTargetDir + "\\" + currentFile.m_strFileName );

                    progressbarStepSizeSum += progressBarStepSize;
                    if (progressBarStepSize < 1 && progressbarStepSizeSum > 1)
                    {
                        // invoke methodUpdateProgressbar asynchronously in main thread to update progress bar
                        mainForm.BeginInvoke(mainForm.methodUpdateProgressbar, new Object[] { 1 });
                        progressbarStepSizeSum = 0;
                    }
                    else
                    {
                        mainForm.BeginInvoke(mainForm.methodUpdateProgressbar, new Object[] { (int)progressBarStepSize });
                    }

                }
                catch (Exception e)
                {
                    // Handle this exception in some way, such as with the following code:
                    //MessageBox.Show(e.Message);
                    Core.ErrorMgmt.Add(new Core.ErrorMsg(e.Message));
                }

            }
            // make sure progressbar is full at the end
            mainForm.BeginInvoke(mainForm.methodUpdateProgressbar, new Object[] { 100 });
        }
    }
}
